package capturador;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 *
 * @author Alejandro D. Villalba
 */
public class LienzoOriginal extends javax.swing.JPanel {

    private Image imagen;
    
    //La imagen en tonos de gris
    private BufferedImage imgGrises;

    //La imagen original
    private BufferedImage imgOriginal;
    
    private int maxWidth;
    private int maxHeight;
    
    public LienzoOriginal() {
        initComponents();
    }

    public void setImage(Image imagen) {
        this.imagen = imagen;
        this.setImgOriginal((BufferedImage) imagen);
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 321, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void paint( Graphics g ) {
        super.paint( g );
        /*if( imagen != null ) {
            g.drawImage(imagen, 0, 0, getWidth(), getHeight(), this);
        }*/
        
        //Se crea un nuevo BufferedImage en tonos de gris
        if(this.getImgOriginal()!=null)
        {    
            //System.out.println(this.getWidth()+";"+this.getHeight());
            this.setImgGrises   (
                                new BufferedImage   (
                                                    this.getWidth(),
                                                    this.getHeight(),
                                                    BufferedImage.TYPE_BYTE_GRAY //BufferedImage.TYPE_INT_RGB
                                                    )
                                );
            //se obtiene la instancia Graphics2D sobre el nuevo BufferedImage en tonos de gris
            Graphics2D graficadorGrises = this.getImgGrises().createGraphics();
            //Se grafica
            graficadorGrises.drawImage(this.imgOriginal,0,0,this.getWidth(),this.getHeight(),null);
            g.drawImage(this.getImgGrises(), 0, 0, this);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    /**
     * @return the imgGrises
     */
    public BufferedImage getImgGrises() {
        return imgGrises;
    }

    /**
     * @param imgGrises the imgGrises to set
     */
    public void setImgGrises(BufferedImage imgGrises) {
        this.imgGrises = imgGrises;
    }

    /**
     * @return the imgOriginal
     */
    public BufferedImage getImgOriginal() {
        return imgOriginal;
    }

    /**
     * @param imgOriginal the imgOriginal to set
     */
    public void setImgOriginal(BufferedImage imgOriginal) {
        this.imgOriginal = imgOriginal;
    }

    /**
     * @return the maxWidth
     */
    public int getMaxWidth() {
        return maxWidth;
    }

    /**
     * @param maxWidth the maxWidth to set
     */
    public void setMaxWidth(int maxWidth) {
        this.maxWidth = maxWidth;
    }

    /**
     * @return the maxHeight
     */
    public int getMaxHeight() {
        return maxHeight;
    }

    /**
     * @param maxHeight the maxHeight to set
     */
    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }


}
